package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.User;
import com.jr.service.IUserService;
import com.jr.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/User/*")
public class UserConyroller extends HttpServlet {
    IUserService ius = new UserServiceImpl();

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
         String uname = request.getParameter("uname");
         String psw = request.getParameter("upassword");
        System.out.println("*****"+uname+"*******"+psw);
         User u = new User();
         u.setUsercode(uname);
         u.setPassword(psw);
        boolean b = ius.queryUserByUser(u);
        System.out.println(b+"***********************");
        response.getWriter().println(new Gson().toJson(b));


    }



}
